package com.westa.smallshop.goodsadd;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseActivity;
import com.westa.smallshop.data.source.remote.GoodsItemRemoteDataSource;
import com.westa.smallshop.data.source.remote.ImageRemoteSource;
import com.westa.smallshop.util.ActivityUtils;

public class GoodsAddActivity extends BaseActivity {

    public static void startActivity(Fragment fragment) {

        Intent intent = new Intent(fragment.getActivity(), GoodsAddActivity.class);
        fragment.startActivity(intent);

    }

    public static void startActivity(Fragment fragment, int requestCode) {

        Intent intent = new Intent(fragment.getActivity(), GoodsAddActivity.class);
        fragment.startActivityForResult(intent, requestCode);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_add);

        GoodsAddFragment fragment = (GoodsAddFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_layout);

        if (fragment == null) {
            fragment = GoodsAddFragment.newInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
                    fragment, R.id.fragment_layout);

        }

        new GoodsAddPresenter(fragment,
                new GoodsItemRemoteDataSource(getApplicationContext()),
                new ImageRemoteSource(getApplicationContext()));

    }

    @Override
    public boolean onBackOrNavigationPressed() {
        GoodsAddFragment fragment = (GoodsAddFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_layout);

        if (fragment != null && fragment.isActive()) {

            fragment.showExitHintDialog();
        }
        return true;
    }
}
